/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client.widgets;

import org.nuxeo.gwt.habyt.app.client.Action;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class ActionWidget extends HTML implements ClickHandler {

    protected Action<?> action;

    protected boolean isActive;

    public ActionWidget(String html, Action<?> action) {
        super(html);
        this.action = action;
        addClickHandler(this);
    }

    public Action<?> getAction() {
        return action;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    @Override
    public void onClick(ClickEvent event) {
        Widget p = getParent();
        if (p instanceof ActionGroup) {
            ((ActionGroup) p).onActivate(this);
        }
        action.execute(this);
    }

    // for debug
    // private void printParentChain(Widget w) {
    // Widget p = w.getParent();
    // while (p != null) {
    // System.out.println(p.getElement().getTagName() + ":"
    // + p.getElement().getId() + "\n" + p.getClass().getName()
    // + "\n================================================");
    // p = p.getParent();
    // }
    // }
}
